package com.dycc.libary.base;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.LayoutRes;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.dycc.libary.Constants;
import com.dycc.libary.R;
import com.dycc.libary.config.MainLooperManage;

import butterknife.ButterKnife;

/**
 * Created by wxk on 2018/1/6 0006.
 */

public abstract class BaseActivity extends AppCompatActivity implements IBaseView {

    protected abstract void initView();

    protected abstract void initData();

    // 屏幕宽度
    public float Width;
    // 屏幕高度
    public float Height;

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        View view = LayoutInflater.from(this).inflate(layoutResID, null);
        setContentView(view);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        Width = dm.widthPixels;
        Height = dm.heightPixels;

    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    public void showToastLong(final String message) {
        MainLooperManage.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(BaseActivity.this, message, Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public void showToastShort(final String message) {
        MainLooperManage.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(BaseActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void showSnackBar(final View baseView, final String message,
                             final int showType, final String action, final int icon,
                             final View.OnClickListener clickListener) {
        MainLooperManage.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Snackbar sb = Snackbar.make(baseView, message, showType);
                sb.setAction(action, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        clickListener.onClick(v);
                    }
                });
                sb.setActionTextColor(Color.YELLOW);

                View view = sb.getView();
                TextView tv = (TextView) view.findViewById(R.id.snackbar_text);
                if (icon != Constants.DEFAUL_ICON) {
                    Drawable d = ContextCompat.getDrawable(BaseActivity.this, icon);
                    d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
                    tv.setCompoundDrawables(d, null, null, null);   // 给TextView左边添加图标
                    tv.setGravity(Gravity.CENTER);  // 让文字居中
                }
                view.setBackground(getDrawable(R.color.normal_snackbar_bg));
                sb.show();
            }
        });
    }


}
